using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE
{
    public class MortalRoadPath : UnitRoadPath
    {
        private List<Vector3> pathList = new List<Vector3>();
        private int nextIndex;
        private float pointStartTime;
        private float nextPointTime;
        private float idleStartTime;
        private float idleTime;
        private bool isEscaping = false;
        private bool isMoving = false;

        protected override void Reset()
        {
            isEscaping = false;
            isMoving = false;
        }
        
        public override void Update()
        {
            base.Update();
            if (idleTime != 0)
            {
                if (TimeManager.logicTime - idleStartTime > idleTime)
                {
                    idleTime = 0;
                    StartRoadPath();
                }
                position = unit.Data.position;
            }
            else
            {
                UpdateRoadPath();
            }

            if (position != unit.Data.position)
            {
                unit.Data.SetTargetForward(position - unit.Data.position);
            }
            
            /*if (position == unit.Data.position)
            {
                isFinish = true;
            }*/
        }

        public override void UpdateData()
        {
            if (isEscaping)
            {
                return;
            }

            if (isMoving)
            {
                return;
            }

            isMoving = true;
            actionName = "Walk";
            actionSpeed = 0.7f;
            pathList = PveManager.Instance.GetMortalRoadPath(unit.Data.position,unit.Data.stage);
            idleTime = Random.Range(1f, 5f);
            idleStartTime = TimeManager.logicTime;
        }

        public override void OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            base.OnTransmit(type, arg);
            if (type == ETransmitType.Escape)
            {
                if (isEscaping)
                {
                    return;
                }

                actionName = "Walk";
                actionSpeed = 1;
                isEscaping = true;
                unit.Data.moveSpeed = 3 * PveUtils.globalRatio;
                idleTime = 0;
                Vector3 currentPosition = unit.Data.position;
                Vector3 targetPosition = (unit.Data.position - unit.Data.battleInSightPosition).normalized * 20 +
                                         unit.Data.position;
                pathList = new List<Vector3>{currentPosition,targetPosition};
                StartRoadPath();
            }
            else if (type == ETransmitType.MortalAction)
            {
                UpdateData();
            }
        }

        private void StartRoadPath()
        {
            nextIndex = 0;
            ToNextPoint();
        }
        
        private void UpdateRoadPath()
        {
            if (pathList.Count > nextIndex )
            {
                if (nextIndex > 0)
                {
                    Vector3 targetPosition = pathList[nextIndex];
                    Vector3 lastPosition = pathList[nextIndex - 1];
                    float percent = (TimeManager.logicTime - pointStartTime) / nextPointTime;
                    if (percent <= 1)
                    {
                        position = lastPosition + (targetPosition - lastPosition) * percent;
                    }
                    else
                    {
                        ToNextPoint();
                    }
                }
                else
                {
                    ToNextPoint();
                }
            }
        }
        
        
        private void ToNextPoint()
        {
            position = pathList[nextIndex];
            if (nextIndex < pathList.Count - 1)
            {
                nextIndex++;
            }
            else
            {
                pathList.Clear();
                isMoving = false;
                UpdateData();
                return;
            }
            Vector3 currentPosition = unit.Data.position;
            Vector3 targetPosition = pathList[nextIndex];
            pointStartTime = TimeManager.logicTime;
            nextPointTime = Vector3.Distance(targetPosition, currentPosition) / unit.Data.moveSpeed;
        }
    }
}